package com.etv.launcher.ui.component.status;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class UIBatteryStatusView extends BaseStatusComponent {

    private static final String TAG = "UIBatteryStatusView";
    private static final int DEFAULT_WIDTH = 140;
    private static final int DEFAULT_HEIGHT = 80;

    private Paint outerCirclePaint;
    private Paint innerCirclePaint;
    private Paint textPaint;
    private float batteryLevel;

    public UIBatteryStatusView(Context context) {
        super(context);
        init();
    }

    public UIBatteryStatusView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public UIBatteryStatusView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setWillNotDraw(false); // 启用onDraw方法

        outerCirclePaint = new Paint();
        outerCirclePaint.setColor(Color.GRAY);
        outerCirclePaint.setStyle(Paint.Style.FILL);
        outerCirclePaint.setAntiAlias(true);

        innerCirclePaint = new Paint();
        innerCirclePaint.setColor(Color.GREEN);
        innerCirclePaint.setStyle(Paint.Style.FILL);
        innerCirclePaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(20);
        textPaint.setTextAlign(Paint.Align.CENTER);

        batteryLevel = 0;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = View.MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = View.MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = View.MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = View.MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        if (widthMode == View.MeasureSpec.EXACTLY) {
            width = widthSize;
        } else if (widthMode == View.MeasureSpec.AT_MOST) {
            width = Math.min(DEFAULT_WIDTH, widthSize);
        } else {
            width = DEFAULT_WIDTH;
        }

        if (heightMode == View.MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == View.MeasureSpec.AT_MOST) {
            height = Math.min(DEFAULT_HEIGHT, heightSize);
        } else {
            height = DEFAULT_HEIGHT;
        }

        Log.d(TAG, "Measured width: " + width + ", height: " + height);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas); // 必须调用父类方法

        // 绘制逻辑（确保颜色和尺寸合理）
        int viewHeight = getHeight();

        // 绘制同心圆
        int circleAreaWidth = 20;
        float centerX = circleAreaWidth / 2f;
        float centerY = viewHeight / 2f;
        float outerRadius = Math.min(circleAreaWidth, viewHeight) / 2f;
        float innerRadius = outerRadius * batteryLevel / 100f;

        // 使用不透明颜色
        outerCirclePaint.setColor(Color.GRAY);
        innerCirclePaint.setColor(Color.GREEN);
        canvas.drawCircle(centerX, centerY, outerRadius, outerCirclePaint);
        canvas.drawCircle(centerX, centerY, innerRadius, innerCirclePaint);

        // 绘制文字
        textPaint.setColor(Color.BLACK);
        float textX = circleAreaWidth*2+10;
        float textY = centerY - (textPaint.descent() + textPaint.ascent()) / 2;
        canvas.drawText(batteryLevel + "%", textX, textY, textPaint);
    }

    public void setBatteryLevel(float level) {
        if (level < 0) {
            level = 0;
        } else if (level > 100) {
            level = 100;
        }
        this.batteryLevel = level;
        invalidate();
    }
}